package org.upt.dcda;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.upt.dcda.db.UserEntity;
import org.upt.dcda.exceptions.BookstoreException;
import org.upt.dcda.exceptions.InvalidCredentialsException;

/**
 * Session Bean implementation class UserManagerBean
 */
@Stateless
public class UserManagerBean implements UserManagerBeanRemote,
		UserManagerBeanLocal
{

	@PersistenceContext
	private EntityManager	entityManager;

	/**
	 * Default constructor.
	 */
	public UserManagerBean()
	{
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public UserEntity createUser(String firstName, String lastName,
			String email, String password, String address)
			throws BookstoreException
	{
		try
		{
			UserEntity ue = new UserEntity(firstName, lastName, email,
					encryptPassword(password), address);
			entityManager.persist(ue);

			return ue;

			// Query query =
			// entityManager.createNamedQuery(UserEntity.CREATE_USER_QUERY);
			// query.setParameter("firstName", firstName);
			// query.setParameter("lastName", lastName);
			// query.setParameter("email", email);
			// query.setParameter("password", password);
			// query.setParameter("address", address);
			//
			// query.executeUpdate();
		}
		catch (Exception e)
		{
			throw new BookstoreException(
					"Caught Exception in UserManagerBean !", e);
		}
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public UserEntity modifyUserInfo(int userId, String firstName,
			String lastName, String address) throws BookstoreException
	{
		try
		{
			UserEntity user = (UserEntity) entityManager.find(UserEntity.class,
					userId);

			user.setUserFirstName(firstName);
			user.setUserLastName(lastName);
			user.setUserAddress(address);

			entityManager.persist(user);
			return user;
		}
		catch (Exception e)
		{
			throw new BookstoreException(
					"Caught Exception in UserManagerBean !", e);
		}
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public UserEntity modifyCredentials(int userId, String currentPassword,
			String newPassword) throws BookstoreException
	{
		try
		{
			Query query = entityManager
					.createNamedQuery(UserEntity.MODIFY_CREDENTIALS_QUERY);

			query.setParameter("id", userId);
			query.setParameter("currentPassword", currentPassword);
			query.setParameter("newPassword", newPassword);

			int updateCount = query.executeUpdate();
			if (updateCount != 1)
			{
				throw new InvalidCredentialsException();
			}

			return (UserEntity) entityManager.find(UserEntity.class, userId);
		}
		catch (InvalidCredentialsException ice)
		{
			throw ice;
		}
		catch (Exception e)
		{
			throw new BookstoreException("Caught exception: " + e.toString(), e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public UserEntity verifyCredentials(String email, String password)
			throws BookstoreException
	{
		try
		{
			Query query = entityManager
					.createNamedQuery(UserEntity.VERIFY_CREDENTIALS_QUERY);

			query.setParameter("email", email);
			query.setParameter("password", encryptPassword(password));

			UserEntity result = (UserEntity) query.getSingleResult();

			return result;
		}
		catch (Exception e)
		{
			throw new BookstoreException("Caught exception: " + e.toString(), e);
		}
	}

	private String encryptPassword(String password)
	{
		return password;
	}
}
